using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    [Serializable]
    public class FileMoveInstruction:InstructionBase
    {
        #region props
        private List<string> _SrcFiles;

        public List<string> SrcFiles
        {
            get { return _SrcFiles; }
            set { _SrcFiles = value; }
        }

        private string _TgtFolder;

        public string TgtFolder
        {
            get { return _TgtFolder; }
            set { _TgtFolder = value; }
        }
	
        #endregion
        public FileMoveInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._SrcFiles = null;
            this._TgtFolder = string.Empty;
        }

        public FileMoveInstruction(Guid componentID, Guid jobTicketID, List<string> srcFiles, string tgtFolder) : base(componentID, jobTicketID)
        {
            this._SrcFiles = srcFiles;
            this._TgtFolder = tgtFolder;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode instructionNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, instructionNode, "TargetFolderPath", this.TgtFolder);
            XmlNode srcFileRootNode = XmlDataUtil.AddElement(ref xDoc, instructionNode, "SourceFiles");
            if(this.SrcFiles !=null && this.SrcFiles.Count>0)
            {
                foreach(string filePath in this.SrcFiles)
                {
                    XmlNode fileNode = XmlDataUtil.AddElement(ref xDoc, srcFileRootNode, "File");
                    XmlDataUtil.UpdateAttribute(ref xDoc,fileNode,"FilePath",filePath);
                }
            }

            return instructionNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileMoveInstruction instruction = new FileMoveInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.TgtFolder = xNode.Attributes.GetNamedItem("TargetFolderPath").Value;
                instruction.SrcFiles = null;
                XmlNodeList fileNodes = xNode.SelectNodes("SourceFiles/File");
                if(fileNodes !=null && fileNodes.Count>0)
                {
                    instruction.SrcFiles=new List<string>();
                    foreach(XmlNode fileNode in fileNodes)
                    {
                        string filePath = fileNode.Attributes.GetNamedItem("FilePath").Value;
                        instruction.SrcFiles.Add(filePath);
                    }
                }
                
                return instruction;
            }
            else
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
        }
    }
}
